package com.example.demo.controller;

import com.example.demo.entity.ResponseInfo;
import com.example.demo.typeHandler.WebSocket;
import com.example.demo.utils.SeleniumUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Validated
public class TestController {

    @Resource
    WebSocket webSocket;

    @PostMapping("/test")
    public ResponseInfo<String> test(@RequestBody String data) {
//        webSocket.sendAllMessage(data);
//        String jsonPathToString = JsonPath.parse(data).read("$..key").toString();
//        return ResponseInfo.successInfo(data);
//        WebDriverManager.chromedriver().forceDownload().setup();
        ChromeOptions options = new ChromeOptions();
        //允许所有请求
        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--headless");
//        System.setProperty("webdriver.chrome.driver","src\\main\\resources\\chromedriver.exe");
        WebDriver webDriver = new ChromeDriver(options);
        SeleniumUtils seleniumUtils = new SeleniumUtils();
        seleniumUtils.chromeDriverMaxSize(webDriver);
        seleniumUtils.get(webDriver, "https://www. baidu.com");
        seleniumUtils.sendKeys(webDriver, "xpath", "//*[@id=\"kw\"]", "text");
        System.out.println("---run end---");
        seleniumUtils.quit(webDriver);
        return ResponseInfo.successInfo("");
    }

    @PostMapping("/formDataTest")
    public ResponseInfo<Long> formDataTest(@ApiParam("测试id") @RequestParam(value = "formDataTest", required = true) Long formDataTest) {
        return ResponseInfo.successInfo(formDataTest);

    }

    @PostMapping("/paramsTest")
    public ResponseInfo<Long> paramsTest(@RequestParam(required = true) Long paramsTest) {
        return ResponseInfo.successInfo(paramsTest);

    }


    @ApiOperation(value = "采购/销售账户-通过客户查询账户列表（一期默认权限）")
    @PostMapping("/accountList")
    public ResponseInfo<List<Long>> accountList(
            @ApiParam("所属租户id") @RequestParam(value = "tenantId", required = true) Long tenantId,
            @ApiParam("客户id") @RequestParam(value = "customerId", required = false) Long customerId) {

        List<Long> paramsTest = new ArrayList<>();
        paramsTest.add(tenantId);
        paramsTest.add(customerId);
        return ResponseInfo.successInfo(paramsTest);
    }

}
